package com.qfedu.service.impl;

import com.qfedu.dao.GradeDao;
import com.qfedu.entity.Grade;
import com.qfedu.entity.GroupGradeData;
import com.qfedu.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class GradeServiceImpl implements GradeService {

    @Autowired
    private GradeDao gradeDao;


    @Override
    public List<Grade> getAllGrade() {
        return gradeDao.getAllGrade();
    }

    @Override
    public int updateGrade(Grade grade) {
        return gradeDao.updateGrade(grade);
    }

    @Override
    public int deleteGradeById(int id) {
        return gradeDao.deleteGradeById(id);
    }

    @Override
    public List<String> getAllGradeName() {
        return gradeDao.getAllGradeName();
    }

    @Override
    public int insertGrade(Grade grade) {
        return gradeDao.insertGrade(grade);
    }

    @Override
    public GroupGradeData getCountByGrade() {
        List<Map> mapList = gradeDao.getCountByGrade();

        List<String> grades = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();

        for (Map map : mapList) {
            grades.add((String) map.get("name"));

            Long num = (Long) map.get("num");
            counts.add(Math.toIntExact(num));
        }

        GroupGradeData groupGradeData = new GroupGradeData();

        groupGradeData.setGrades(grades);
        groupGradeData.setCounts(counts);

        return groupGradeData;
    }
}
